define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'spider/spider/index' + location.search,
                    // add_url: 'spider/spider/add',
                    // edit_url: 'spider/spider/edit',
                    del_url: 'spider/spider/del',
                    multi_url: 'spider/spider/multi',
                    import_url: 'spider/spider/import',
                    detail_url: 'spider/spider_prices/index',
                    table: 'spider',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'task_id', title: __('Task_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'local_img_url', title: __('商品本地图'), operate: false, table: table, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'price', title: __('Price'), operate: 'LIKE', formatter: Table.api.formatter.label},
                        {field: 'page_title', title: __('title标签'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'meta_description', title: __('meta的描述'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'store_url', title: __('店铺地址'), operate: false, table: table, class: 'autocontent', formatter: Table.api.formatter.url},
                        {field: 'shop_description', title: __('商品描述'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'images', title: __('商品图'), operate: false, table: table, formatter: Table.api.formatter.url},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'task.id', title: __('Task.id')},
                        // {field: 'task.user_id', title: __('Task.user_id')},
                        // {field: 'task.get_url', title: __('Task.get_url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        // {field: 'task.use_time', title: __('Task.use_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'task.status', title: __('Task.status')},
                        // {field: 'task.updatetime', title: __('Task.updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'task.createtime', title: __('Task.createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                                {
                                    name: 'detail',
                                    text: __('商品详情'),
                                    title: __('商品详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    extend: "data-area='[\"100%\",\"100%\"]'",
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.detail_url + "?shop_uniqid=" + row.shop_uniqid;
                                    },
                                    // visible: function (row) {
                                    //     //返回true时按钮显示,返回false隐藏
                                    //     return true;
                                    // }
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
